//
//  SideCategoryController.m
//  BuyOfNoWane
//
//  Created by qianfeng02 on 15/7/9.
//  Copyright (c) 2015年 迷茫的尧尧. All rights reserved.
//

#import "SideCategoryController.h"
#import "ILBarButtonItem.h"
#import "BaseViewController.h"
#import "CategryCell.h"
@interface SideCategoryController ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
{
    UISearchBar *_searchbar;
    UICollectionView *_collectionView;
    NSArray *_titleArray;
    NSArray *_imageName;
}
@end

@implementation SideCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self showCategoryUI];
    self.navigationController.navigationBar.backgroundColor=[UIColor cyanColor];
    
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
}

-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    [_searchbar resignFirstResponder];
}
#pragma mark -searchbar delegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    
    searchBar.showsCancelButton = NO;
    return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
    return YES;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.text=@"";
    [searchBar resignFirstResponder];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //点击search调用
//    NSString *str=[searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString *url=[NSString stringWithFormat:kSearchUrl,self.categoryType,str];
//    
//    [_dataArray removeAllObjects];
//    [self addTaskWithUrl:url isRefresh:YES];
//    [self.tableView reloadData];
    NSLog(@"search click");
     [_searchbar resignFirstResponder];
  
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_searchbar resignFirstResponder];
}
-(void)createUI
{
    CGRect mainViewBounds = self.navigationController.view.bounds;
    _searchbar = [[UISearchBar alloc] initWithFrame:
                  CGRectMake(CGRectGetMinX(mainViewBounds)+40,
                             CGRectGetMinY(mainViewBounds)+20,
                             self.navigationController.view.bounds.size.width-50,
                             44)];
    _searchbar.delegate = self;
    [self.navigationController.view addSubview: _searchbar];
    _searchbar.backgroundColor=[UIColor cyanColor];
    _searchbar.placeholder=@"买不了吃亏哟，搜搜看~~";
    _searchbar.barTintColor=[UIColor whiteColor];
    _searchbar.layer.masksToBounds = YES;
    [_searchbar.layer setCornerRadius:10.0];
    [_searchbar.layer setBorderColor:nil];
    ILBarButtonItem *backbtn=
    [ILBarButtonItem barItemWithImage:[UIImage imageNamed:@"ic_back_s"]
                        selectedImage:[UIImage imageNamed:@"ic_back_s"]
                               target:self
                               action:@selector(backbtnClick:)];
    [self.navigationItem setLeftBarButtonItem:backbtn];
}

-(void)backbtnClick:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
    _searchbar.hidden=NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden=NO;
    _searchbar.hidden=YES;
    self.navigationController.navigationBar.backgroundColor=nil;
}
//分类信息
#pragma mark  分类信息
-(void)showCategoryUI
{
    _titleArray=[[NSArray alloc]initWithObjects:@"电脑数码",@"家用电器",@"服饰鞋包",@"个护化妆",@"母婴用品",@"日用百货",@"食品保健",@"礼品钟表",@"图书音像",@"玩模乐器",@"家居家装",@"办公设备",@"汽车用品",@"其他",nil];
    _imageName=[[NSArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"", nil];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    // 设置行间距
    flowLayout.minimumLineSpacing = 0;
    // 设置item之间间距，实际间隔需要根据item的size和edgesInsets的值来调节
    flowLayout.minimumInteritemSpacing = 0;
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, kScreenSize.width, 20)];
    title.text=@"商品分类";
    [self.view addSubview:title];
     title.backgroundColor=[UIColor magentaColor];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 84, kScreenSize.width, kScreenSize.height-49-84) collectionViewLayout:flowLayout];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    _collectionView.backgroundColor=[UIColor clearColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"CategryCell" bundle:nil] forCellWithReuseIdentifier:@"CategryCell"];
    [self.view addSubview:_collectionView];
    _collectionView.layer.masksToBounds=YES;  
    [_collectionView.layer setBorderColor:[UIColor blackColor].CGColor];
}
#pragma mark - CollectionView Delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  _titleArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategryCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CategryCell" forIndexPath:indexPath];
    cell.categoryLabel.text=_titleArray[indexPath.row];
    cell.iamgeView.image=nil;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenSize.width/3,kScreenSize.width/3   );
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
